# -*- coding: UTF-8 -*-
import urllib
import urllib2
import cookielib
from HTMLParser import HTMLParser
import re
import xlrd
import xlwt
import string
#=============================================================================

class fdcinfo(object):
    objname=''
    companyname=''
    number=''
    pos=''




#=============================================================================
class myHtmlParser(HTMLParser):
    irstart=0
    irend=0
    result=[]
    data=[]
    iscleasspace=0
    def __init__(self):
        HTMLParser.__init__(self)
        self.flag=None
    def handle_starttag(self,tag,attrs):
        if self.irstart==1:
            if tag=='a':
                self.flag='a'
                for href,link in attrs:
                    if href=='href':
                        if link.find('lp_detailinfo')>=0:
                            strs = link.split('id=')
                            strs = strs[1].split('&')
                            self.result.append(strs[0])





    def handle_data(self,data):
        if self.irend==1:
            sdata = data.strip()
            if sdata!='':
                if self.iscleasspace==1:
                    sdata=sdata.replace("\r\n","")
                    sdata=sdata.replace(" ","")
                    sdata=sdata.replace(' ','')
                self.data.append(sdata)
                #print 'data:'+sdata

#=============================================================================
def getfdcinfo(fdcnumber):
    url = 'http://house.shunde.gov.cn/lp_detailinfo.jsp?id='+fdcnumber+'&type=1'
    print url
    response = urllib2.urlopen(url)
    m= myHtmlParser()
    m.irend=1
    m.iscleasspace=1
    fstr = response.read()
    m.feed(fstr)
    response.close()
    m.close()
    result=m.data
    readindx =0


    fif= fdcinfo()
    while readindx<len(result):
        kvalue = result[readindx]
        if kvalue == '开发企业名称':
            kvalue = result[readindx+1]
            fif.companyname = kvalue
        if kvalue == '项目名称':
            kvalue = result[readindx+1]
            fif.objname = kvalue
        if kvalue == '项目坐落':
            kvalue = result[readindx+1]
            fif.pos = kvalue
        readindx+=1
    fif.number = fdcnumber
    print '开发企业名称'+fif.companyname
    print '项目名称'+fif.objname
    print '项目坐落'+fif.pos

    return fif



def getfdcsellinfo(fdcnumber):
    url = 'http://house.shunde.gov.cn/lp_xs_detailinfo.jsp?id='+fdcnumber+'&type=1'
    print url
    response = urllib2.urlopen(url)
    m= myHtmlParser()
    m.irend=1
    fstr = response.read()
    m.feed(fstr)
    response.close()
    m.close()
    result=m.data
    starti=0
    endi=0
    vindx=0
    #预售
    for item in result:
        findstr = unicode(item,'utf-8')
        if findstr==u'房屋用途':
            starti = vindx
        if findstr==u'面积单位：平方米 金额单位：万元 均价单位：元/平方米':
            endi=vindx

        vindx+=1
    vindx=starti
    datas=[]
    while vindx<endi:
        datas.append(result[vindx])
        vindx+=1
    #====================================
    #获取头
    resultDic={}
    headlist=[]
    findx = 0
    while findx<10:
        headlist.append(datas[findx])
        findx+=1
    findx=10
    keyindx=0
    rowkey=''
    while findx<len(datas):
        if keyindx>=10:
            keyindx=0
        itemDic ={}

        kkey = headlist[keyindx]#获取头
        kvalue = datas[findx]#获取值
        if keyindx==0:
            rowkey =kvalue#如果是第一个，则是
            if rowkey=='面积':
                rowkey = datas[findx-10]+rowkey
        else:
            if resultDic.has_key(kkey):
                itemDic = resultDic[kkey]
            else:
                resultDic[kkey]=itemDic
            itemDic[rowkey] = kvalue.strip()
        keyindx+=1
        findx+=1
    resultDic['id']=fdcnumber
    return resultDic






def getPageList(pcount):
    allresults=[]
    indx=1
    while indx<=23:
        cookie = cookielib.CookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
        url='http://house.shunde.gov.cn/servlet/list'
        values={
            'o':'getJSON','p':str(indx),'template_type':'lpxs_xs_lpld_list'}
        data = urllib.urlencode(values)
        req = urllib2.Request(url,data)
        req.add_header('Host','house.shunde.gov.cn')
        req.add_header('Referer','http://house.shunde.gov.cn/')
        req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0')
        try:
            response = opener.open(req)
        except :
            continue
        the_page=response.read()
        # print  the_page
        m= myHtmlParser()
        m.irstart=1
        m.feed(the_page)
        m.close()
        allresults = m.result
        indx+=1
    indx =0
    return allresults

print '============='


#==========================================================================

allfdcnumbers = getPageList(7)
print '================:'+str(len(allfdcnumbers))
#==========================================================================
#统计
#==========================================================================
resultDic={}

f= xlwt.Workbook()#创建工作簿
sheetname = "成交公示"

salletype=[]

zz=u'住宅'
sy=u'商业'
xzl=u'写字楼'
cw=u'车位'
ck=u'车库'
mtcw=u'摩托车位'
zwj=u'杂物间'
qt=u'其他'
hj=u'合计'

salletype.append(zz)
salletype.append(sy)
salletype.append(xzl)
salletype.append(cw)
salletype.append(ck)
salletype.append(mtcw)
salletype.append(zwj)
salletype.append(qt)
salletype.append(hj)




zksts =u'总可售套数'
zkstsmj=u'总可售套数面积'
ljxsts=u'累计销售套数'
ljxsmj=u'累计销售面积'
xsje=u'销售金额'
xsjj=u'销售均价'
ksts=u'当前可售套数'
kstsmj=u'当前可售套数面积'

nrowsdic={}

allfdcnumbers =  list(set(allfdcnumbers))

limitindex =0
posindex=0
for k in allfdcnumbers:
    print '=================================='
    #if limitindex>=1:
    #    break
    limitindex+=1
    result=getfdcsellinfo(k)
    id = result['id']
    fif = getfdcinfo(id)
    #判断时间(年)
    nmj=0
    nkl=0
    sheName = id[0:4]
    for reskey in result:
        if reskey=='id':
            continue
        print reskey
        nrows = 1

        lks = unicode(reskey,'utf-8')
        if nrowsdic.has_key(lks):
            nrows = nrowsdic[lks]

        if resultDic.has_key(lks):
            sheet = resultDic[lks]
        else:
            sheet = f.add_sheet(lks,cell_overwrite_ok=True)
            #写入头
            #写入行
            sheet.write(0,0,u'项目名称')
            sheet.write(0,1,u'开发企业')
            sheet.write(0,2,u'项目坐落')
            sheet.write(0,3,u'时间')
            sheet.write(0,4,u'预售证')
            sheet.write(0,5,zksts)
            sheet.write(0,6,zkstsmj)
            sheet.write(0,7,ljxsts)
            sheet.write(0,8,ljxsmj)
            sheet.write(0,9,xsje)
            sheet.write(0,10,xsjj)
            sheet.write(0,11,ksts)
            sheet.write(0,12,kstsmj)
            resultDic[lks] = sheet
        #获取行数
        #写入信息
        itemddc = result[reskey]#获取到一类
        sheet.write(nrows,0,unicode(fif.objname,'utf-8'))
        sheet.write(nrows,1,unicode(fif.companyname,'utf-8'))
        sheet.write(nrows,2,unicode(fif.pos,'utf-8'))
        sheet.write(nrows,3,sheName)
        sheet.write(nrows,4,id)
        sheet.write(nrows,5,unicode(itemddc[zksts.encode("utf8")],'utf-8'))
        sheet.write(nrows,6,unicode(itemddc[zkstsmj.encode("utf8")],'utf-8'))
        sheet.write(nrows,11,unicode(itemddc[ksts.encode("utf8")],'utf-8'))
        sheet.write(nrows,12,unicode(itemddc[kstsmj.encode("utf8")],'utf-8'))
        #销售套数
      #  numkey = u'已预定套数'
       # vks = itemddc[numkey.encode("utf8")]
       # vks =unicode(vks,'utf-8')
       # vks.strip()
       # nkl= string.atoi(vks)
        nkl=0
        numkey = u'已签约套数'
        vks = itemddc[numkey.encode("utf8")]
        vks =unicode(vks,'utf-8')
        vks.strip()
        nkl = string.atoi(vks)

       # numkey = u'已备案套数'
      #  vks = itemddc[numkey.encode("utf8")]
       # vks =unicode(vks,'utf-8')
       # vks.strip()
       # nkl += string.atoi(vks)

        #销售面积
       # numkey = u'已预定套数面积'
     #   vksmj = itemddc[numkey.encode("utf8")]
      #  vksmj =unicode(vksmj,'utf-8')
      #  vksmj.strip()
      #  nmj = string.atof(vksmj)

    #    numkey = u'已备案套数面积'
     #   vksmj = itemddc[numkey.encode("utf8")]
    #    vksmj =unicode(vksmj,'utf-8')
     #   vksmj.strip()
    #    nmj += string.atof(vksmj)
        nmj=0
        numkey = u'已签约套数面积'
        vksmj = itemddc[numkey.encode("utf8")]
        vksmj =unicode(vksmj,'utf-8')
        vksmj.strip()
        nmj = string.atof(vksmj)

        sheet.write(nrows,7,str(nkl))
        sheet.write(nrows,8,str(nmj))
        #销售金额
        sheet.write(nrows,9,unicode(itemddc[xsje.encode("utf8")],'utf-8'))
        sheet.write(nrows,10,unicode(itemddc[xsjj.encode("utf8")],'utf-8'))
        #
        nrows+=1
        nrowsdic[lks]= nrows
        #============
    #写入
    posindex+=1
    #print str(len(result))


sheetnameDic={}

f.save( u'顺德商品房现售.xls')

